<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    width="800px"
    top="50px"
    append-to-body
    :show-close="showClose"
  >
  <!-- 活动充值界面 -->
  <div class="chongzhi-box">
    <!-- <div class="top-box flex" >
      <div class="item" v-for="(item,index) in huodong" :key="index">
        <div class="item-top flex fbt">
          <span class="money">充{{activeInfo&&activeInfo.mode==1? item.minAmount:item.maxAmount}}元</span>
          <span class="flex">
            <span class="clock"></span>
            <span class="xs">限时</span>
          </span>
        </div>
        <div class="item-bot">
          得
          <span class="jf">
            {{(activeInfo&&activeInfo.mode==1? item.minAmount:item.maxAmount)*10*item.multiple+Number(item.pointsNum)}}虞豆
          </span>
        </div>
        <div class="name">
          活动{{index+1}}
        </div>
      </div>
    </div>

      <div class="des" v-if="activeRemark">

        <div class="des-cont" v-html="activeRemark">

        </div>
      </div>
      <div class="des">
        <div class="tit">如何免费获取?</div>
        <div class="des-cont" v-html="desCont">

        </div>
      </div> -->
      <!-- <div class="sub-tit font16 bold">充值虞豆</div> -->
       <div v-if="shopInfo&&shopInfo.campNumber < -100" style="font-size: 18px;">您已欠 <span style="color:red;font-size: 22px;">{{ shopInfo.campNumber }}</span> 虞豆,请尽快充值!</div>
      <div class="kuaijie flex ">
        <div
          class="kuaijie-item"
          @click="kuaijieClick(activeInfo&&activeInfo.mode==1? item.minAmount:item.maxAmount, i)"
          :class="{ active: form.price == item.minAmount}"
          v-for="(item, i) in huodong"
          :key="i"
        >
          <div class="money font16">{{activeInfo&&activeInfo.mode==1? item.minAmount:item.maxAmount }}元</div>
          <div class="jf font12">{{ (activeInfo&&activeInfo.mode==1? item.minAmount:item.maxAmount)*10*item.multiple+Number(item.pointsNum) }}虞豆</div>
          <div class="remark" v-html="item.remark"></div>
          <div class="xs">限时</div>
        </div>
      </div>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="135px"
        class="demo-ruleForm"
      >
        <el-form-item label="自定义充值金额:" prop="price">
          <el-input-number
            :controls="false"
            v-model="form.price"
            placeholder="请输入充值金额"
            @change="getCountInfoByType"

          />
          <span v-if="countJifen" style="margin-left:10px">到账 <strong style="font-size:18px;color:#ff9717">{{countJifen}}</strong> 虞豆</span>
        </el-form-item>
        <el-form-item label="支付方式：" prop="num">
          <payWayLsit :showYue="true" :payWay='payWay' ref="payWayLsit" @selectPayway="selectPayway"></payWayLsit>
        </el-form-item>
        <div class="tip">
          说明：1元可充值10虞豆 虞豆不可提现 虞豆可以用于发布工具自动获取 渠道接口开通等其他板块使用。
        </div>
        <div class="center">
          <el-button
            type="primary"
            @click="submitForm('form')"
            v-loading="loading"
            >确认充值</el-button
          >
        </div>
      </el-form>
      <payQrcode ref="payQrcode"   @checkOrderInterval="checkOrderInterval" @checkOrder="checkOrder"></payQrcode>
    </div>
  </el-dialog>
</template>
<script>
import moneyApi from "@/api/financeCenter/myMoney";
import payApi from "@/api/financeCenter/pay";
import payQrcode from "./PayQrcodeCommon.vue"
import payWayLsit from "../../components/payWayLsit.vue"
import _ from 'lodash'
export default {
  props: {
    title: {
      type: String,
      default: "充值虞豆"
    },
    showClose: {
      type: Boolean,
      default: true
    },
    toPath: String
  },
  computed: {
     shopInfo() {
      return this.$store.getters.shopInfo;
    },
  },
  name: "addjifen",
  components:{payQrcode,payWayLsit},
  data() {

    return {
      dialogVisible: false,
      loading: false,
      activeIndex: "",
      activeInfo:null,
      countJifen:'',
      payWay:2,
      huodong:[],
      desCont:`1.1.分销获取  说明：通过虞都互联系统成交的所有订单将按照订单金额百分之十赠送虞豆<br/>
举例:虞都平台当月帮您售卖10万元的账号销售额,您可获得1万虞豆.<br/>
2.供货获取  说明：在虞都互联系统销售出去的账号订单金额<br/>
举例：您在虞都互联卖自己的号交易金额10万则赠送1万虞豆 按照交易金额的百分之十赠送虞豆 <br/>
3.每月1-5日为虞豆兑换日 兑换商务联系方式19139006222同微信 <br/>
4.最终解释权归虞都所有.`,
        activeRemark:'',
      // 表单参数
      form: {
        price: "",
      },
      orderId:'',
      rules: {
        price: [{ required: true, message: "请输入充值金额", trigger: "blur" }],
      },
      jieliu:0
    };
  },
  created() {
  },
  methods: {
    // 打开弹窗
    open() {
      this.activeIndex = ''
      this.form.price = ''
      this.loading = false;
      this.dialogVisible = true;
      this.activeIndex = 0;

      this.activitySettingsGetByType()
    },
    // 获取活动信息
    activitySettingsGetByType(){
      moneyApi.activitySettingsGetByType({
        type:1
      }).then(res=>{
        if(res.code==200){
          // 将/n换成<br/>
          let data = res.data
          this.activeRemark =  data.remark.replace(/\n/g, '<br/>')
          this.activeInfo = data
          this.huodong = JSON.parse(data.rule)
          this.form.price =  this.activeInfo&& this.activeInfo.mode==1? this.huodong[0].minAmount:this.huodong[0].maxAmount
          this.countComputed()
        }
      })
    },
    selectPayway(val){
      this.payWay = val
    },
    // 根据金额计算应得虞豆
   getCountInfoByType(val){
    if(!val){
      this.countJifen = 0
      return
    }
    if(val<10){
      return this.$message.error('充值金额不能小于10元！')
    }
    if(this.jieliu){
      clearTimeout(this.jieliu)
    }
      this.jieliu = setTimeout(()=>{
        this.countComputed()
      },500)
    },
    countComputed(){
      moneyApi.getCountInfoByType({price:this.form.price}).then(res=>{
          this.countJifen = res.data.count
          this.jieliu = 0
        })
    },

    // 重置
    reset() {
      this.$refs.form.resetFields();
    },
    kuaijieClick(money, i) {
      this.activeIndex = i;
      this.form.price = money;
      this.$refs["form"].validate()
      this.countComputed()
    },
    // 确认充值
    async submitForm(formName) {
      if(this.form.price<10){
        return this.$alert("充值金额不能小于10元！");
      }
      this.$refs["" + formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$confirm(`确认充值${this.form.price}元到虞豆吗？`, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              this.form.mallTitle = '虞豆充值'
              moneyApi.addJifen(this.form).then(async (res) => {
                this.loading = false;
                // return alert(res.data.content)
                this.orderId = res.data.id
                await this.payNow(res.data.id)
                this.loading = false;
                this.dialogVisible = false;
                this.reset();
              });
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          this.$alert("请输入充值金额！");
        }
      });
    },
    // 轮询订单支付结果
    checkOrderInterval(){
      moneyApi
        .queryJifenPayStateInterval({ id: this.orderId })
        .then((res) => {
          if (res.code == 200) {
            if (res.data.payState == 2) {
              this.$refs.payQrcode.showSuccess('虞豆充值','虞豆充值成功，快去使用吧！')
              this.$refs.payQrcode.close()
              this.$store.dispatch("getShopInfo")
              this.$router.push(this.toPath)
            }
          }
        });
    },
    // 查询订单状态
    checkOrder() {
      moneyApi
        .queryJifenPayState({
          id: this.orderId
        })
        .then((res) => {
          if (res.code == 200) {
            if (res.data.payState == 2) {
              this.$refs.payQrcode.showSuccess('虞豆充值','虞豆充值成功，快去使用吧！')
              this.$refs.payQrcode.close()
              this.$store.dispatch("getShopInfo")
              this.$router.push(this.toPath)
            }else{
              this.$message.waring('未查询到支付成功！如有问题请联系客服')
            }
          }
        });
    },
    // 请求支付参数
    async payNow(id){
      let obj = {
              id:id,
              payWay:this.payWay,
              payScene:3,
              qrPayMode:4,
              goodsTitle:'虞豆充值'
            }
            const res1 = await payApi.payNow(obj);
            //console.log(res1);
            if (res1.code === 200) {
              const isHuifu = res1.data.isHuifu||1
              this.$refs.payQrcode.open(this.payWay,res1.data.formStr,isHuifu )
            }
    },

    close() {
      this.reset();
      this.dialogVisible = false;
    },
  },
};
</script>
<style scoped lang="scss">
.chongzhi-box {
  padding: 10px 10px;
  .top-box{
    .flex{
        align-items: center;
      }
      flex-wrap: wrap;
    .item{
      flex-shrink: 0;
      width: 217px;
      height: 110px;
      margin-right: 20px;
      background-image: url("https://imgage.maimaihao.com/yuduhulian/erp/static/icon/jf-bg.png");
      background-size: 100% 100%;
      padding:5px 30px 0 10px;
      margin-bottom: 10px;
      position: relative;
      &:nth-of-type(3n){
        margin-right: 0;
      }
      .money{
        color: #fff;

      }

      .clock{
        width: 18px;
        height: 18px;
        background-image: url("https://imgage.maimaihao.com/yuduhulian/erp/static/icon/clock.png");
        background-size: 100% 100%;
        margin-right: 3px;
      }
      .xs{

        font-size: 16px;
        font-weight: 800;
        color: #333333;
        background: linear-gradient(180deg, #F2E36C 0%, #D1A80D 100%);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        line-height: 16px;
      }
      .name{
        position: absolute;
        right: 0;
        bottom: 0;
        width: 60px;
        height: 18px;
        background: #FFFFFF;
        border-radius: 4px 0px 9px 0px;
        border: 1px solid #CBCAFB;
        text-align: center;
        color: #FF0000;
        font-size: 12px;
      }
      .item-bot{
        color: #000;
        margin-top: 20px;

        .jf{

          font-size: 24px;
          font-weight: 800;

        }
      }
    }
  }
  .des{
    margin-top: 10px;
    .tit{
      font-weight: bold;
    }
  }



  .sub-tit {
    font-size: 24px;
    margin-top: 10px;
  }
  .kuaijie {
    margin-top: 20px;
    flex-wrap: wrap;


  }
  .kuaijie-item {
    min-width: 200px;
    // width: 100%;
    // height: 49px;
    border-radius: 4px;
    border: 1px solid $yellow;
    text-align: center;
    color: $yellow;
    padding: 10px;
    margin-right: 30px;
    margin-bottom: 10px;
    cursor: pointer;
    position: relative;
    &.active {
      color: #fff;
      background-color: #ff9717;
    }
    .xs{
      background-color: #eb8200;
      width: 33px;
      height: 17px;
      font-size: 12px;
      position: absolute;
      top: 0;
      right: 0;
      color: #fff;
    }
  }
  .demo-ruleForm {
    margin-top: 20px;
  }
  .zfb {
    width: 42px;
    height: 42px;
  }
  .tip {
    font-size: 12px;
    color: rgba(0, 0, 0, 0.5);
    margin-top: -15px;
  }
  .center {
    text-align: center;
    margin-top: 20px;
    .el-button {
      width: 293px;
      height: 48px;
      font-size: 18px;
    }
  }
}

</style>
